package com.ccit.service.impl;

import com.ccit.dao.ApprovalDao;
import com.ccit.dao.AttendanceDao;
import com.ccit.dao.UserDao;
import com.ccit.domain.*;
import com.ccit.service.ApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
public class ApprovalServiceImpl implements ApprovalService {

    @Autowired
    private ApprovalDao approvalDao;

    @Autowired
    private AttendanceDao attendanceDao;

    @Autowired
    private UserDao userDao;

    @Override
    public boolean add(Approval approval) {
        return approvalDao.add(approval) > 0;
    }

    @Override
    public boolean addPunch(Punch punch) {
        return approvalDao.addPunch(punch) > 0;
    }

    @Override
    public boolean update(Approval approval) {
        return approvalDao.update(approval) > 0;
    }

    @Override
    public boolean update(Punch punch) {
        //“Punch{id=4, employeeId=1004, employeeName='赵六', startDate='2023-10-31 08:06:00', endDate='2023-10-31 17:32:00', status='已批准'}
//        Punch{id=20, employeeId=1004, employeeName='赵六', startDate='2023-10-16 08:00:00', endDate='2023-10-16 18:00:00', status='已批准'}
        if (punch.getStatus().equals("已批准")) {
            //根据ID和如期来查询出勤表对应的信息
            AttendanceType attendanceRecord = attendanceDao.findByEmployeeIdAndDate(punch.getEmployeeId(), punch.getStartDate());
            System.out.println(attendanceRecord);
                System.out.println("....");
                //更新考勤表的时间
                approvalDao.updateAttendance(punch);
                //更新状态
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                LocalDateTime checkInTime = LocalDateTime.parse(punch.getStartDate(), formatter);
                LocalDateTime checkOutTime = LocalDateTime.parse(punch.getEndDate(), formatter);
                LocalDateTime startDate = LocalDateTime.parse(punch.getStartDate(), formatter);
                LocalDate date = startDate.toLocalDate();
                String formattedDate = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

                AttendanceType attendanceType = attendanceDao.findByEmployeeIdAndDate(punch.getEmployeeId(), formattedDate);

                List<Time> time = userDao.getTime();

                LocalDateTime morningStart = LocalDateTime.of(
                        checkInTime.toLocalDate(),
                        LocalTime.parse(time.get(0).getMorningStart())
                );
                LocalDateTime afternoonEnd = LocalDateTime.of(
                        checkOutTime.toLocalDate(),
                        LocalTime.parse(time.get(0).getAfternoonEnd())
                );
                LocalDateTime morningEnd = LocalDateTime.of(
                        checkInTime.toLocalDate(),
                        LocalTime.parse(time.get(0).getMorningEnd())
                );
                LocalDateTime afternoonStart = LocalDateTime.of(
                        checkOutTime.toLocalDate(),
                        LocalTime.parse(time.get(0).getAfternoonStart())
                );
                String attendanceStatus;
                if (checkInTime.isBefore(morningStart) && checkOutTime.isAfter(afternoonEnd)) {
                    attendanceStatus = "正常";
                } else if (checkInTime.isAfter(morningStart) && checkInTime.isBefore(morningEnd) && checkOutTime.isAfter(afternoonEnd)) {
                    attendanceStatus = "迟到";
                } else if (checkInTime.isBefore(morningStart) && checkOutTime.isAfter(afternoonStart) && checkOutTime.isBefore(afternoonEnd)) {
                    attendanceStatus = "早退";
                } else {
                    attendanceStatus = "缺勤";
                }
                attendanceType.setStatus(attendanceStatus);
                attendanceDao.update(attendanceType);
        }

        return approvalDao.updatePunch(punch) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return approvalDao.delete(id) >0;
    }

    @Override
    public List<Approval> selectAll() {
        return approvalDao.selectALl();
    }

    @Override
    public List<Approval> getById(Integer id) {
        return approvalDao.getById(id);
    }

    @Override
    public List<Punch> getPunchById(Integer id) {
        return approvalDao.getPunchById(id);
    }


    @Override
    public PageBean<Approval> selectByPage(int currentPage, int pageSize) {
        //计算开始索引
        int begin = (currentPage-1 ) * pageSize;
        //计算查询条目数
        int size = pageSize;
        //查询数据记录
        List<Approval> approvals = approvalDao.selectByPage(begin, size);
        //查询总记录数
        int totalCount = approvalDao.selectTotalCount();
        //封装pageBean对象
        PageBean<Approval> pageBean = new PageBean<>();
        pageBean.setRows(approvals);  //rows
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }

    @Override
    public PageBean<Punch> selectByPunchPage(int currentPage, int pageSize) {
        //计算开始索引
        int begin = (currentPage-1 ) * pageSize;
        //计算查询条目数
        int size = pageSize;
        //查询数据记录
        List<Punch> punchs = approvalDao.selectByPunchPage(begin, size);
        //查询总记录数
        int totalCount = approvalDao.selectPunchTotalCount();
//        int totalCount = punchs.size();
        //封装pageBean对象
        PageBean<Punch> pageBean = new PageBean<>();
        pageBean.setRows(punchs);  //rows
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }

    @Override
    public List<Approval> selectByDate(String startDate, String endDate) {
        return approvalDao.selectByDate(startDate, endDate);
    }

}
